package src.dual_pointers;

import java.util.Arrays;

/**
 * @author starsea
 * @date 2024-09-01 19:45
 */

public class Test12 {
    public static void main(String[] args) {
        int[] arr={3,4,-1,1};
        int n=firstMissingPositive(arr);
    }
    public static int firstMissingPositive(int[] nums) {
        Arrays.sort(nums);
        int sum = 1;
        int tmp=0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= 0) {
                continue;
            }
            tmp=i;
            break;
        }
        int left=tmp;
        int right=nums.length-1;
        while(left<right)
        {
            int mid=left+(right-left)/2;
            if(nums[mid]==mid)
            {
                left=mid+1;
            }
            else
            {
                right=mid;
            }
        }
        if(nums[left-tmp]==left)
        {
            return left+1;
        }
        return left;
    }
}
